package com.nbcio.pm.controller;

import com.nbcio.common.core.domain.R;
import com.nbcio.common.log.annotation.Log;
import com.nbcio.common.log.enums.BusinessType;
import com.nbcio.common.mybatis.core.page.PageQuery;
import com.nbcio.common.mybatis.core.page.TableDataInfo;
import com.nbcio.common.web.core.BaseController;
import com.nbcio.pm.domain.bo.PmTimeTrackingBo;
import com.nbcio.pm.domain.vo.PmTimeStatsVo;
import com.nbcio.pm.domain.vo.PmTimeTrackingVo;
import com.nbcio.pm.service.IPmTimeTrackingService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pm/time")
@Tag(name = "时间跟踪", description = "时间跟踪相关接口")
public class PmTimeTrackingController extends BaseController {

    private final IPmTimeTrackingService pmTimeTrackingService;

    @Operation(summary = "分页查询时间记录")
    @SaCheckPermission("pm:time:list")
    @GetMapping("/list")
    public TableDataInfo<PmTimeTrackingVo> list(PmTimeTrackingBo bo, PageQuery pageQuery) {
        return pmTimeTrackingService.queryPageList(bo, pageQuery);
    }

    @Operation(summary = "开始计时")
    @SaCheckPermission("pm:time:add")
    @Log(title = "时间跟踪", businessType = BusinessType.INSERT)
    @PostMapping("/start")
    public R<Void> start(@RequestBody PmTimeTrackingBo bo) {
        return toAjax(pmTimeTrackingService.startTracking(bo));
    }

    @Operation(summary = "停止计时")
    @SaCheckPermission("pm:time:edit")
    @Log(title = "时间跟踪", businessType = BusinessType.UPDATE)
    @PostMapping("/stop/{id}")
    public R<Void> stop(@PathVariable Long id) {
        return toAjax(pmTimeTrackingService.stopTracking(id));
    }

    @Operation(summary = "新增或编辑记录")
    @SaCheckPermission("pm:time:edit")
    @Log(title = "时间跟踪", businessType = BusinessType.UPDATE)
    @PostMapping
    public R<Void> save(@RequestBody PmTimeTrackingBo bo) {
        if (bo.getId() == null) {
            return toAjax(pmTimeTrackingService.insertByBo(bo));
        } else {
            return toAjax(pmTimeTrackingService.updateByBo(bo));
        }
    }

    @Operation(summary = "删除记录")
    @SaCheckPermission("pm:time:remove")
    @Log(title = "时间跟踪", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        return toAjax(pmTimeTrackingService.deleteWithValidByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "用户时间统计")
    @SaCheckPermission("pm:time:query")
    @GetMapping("/stats/user")
    public R<PmTimeStatsVo> userStats(@RequestParam Long userId,
                                      @RequestParam LocalDate startDate,
                                      @RequestParam LocalDate endDate) {
        return R.ok(pmTimeTrackingService.getUserTimeStats(userId, startDate, endDate));
    }

    @Operation(summary = "项目时间统计")
    @SaCheckPermission("pm:time:query")
    @GetMapping("/stats/project")
    public R<PmTimeStatsVo> projectStats(@RequestParam Long projectId,
                                         @RequestParam LocalDate startDate,
                                         @RequestParam LocalDate endDate) {
        return R.ok(pmTimeTrackingService.getProjectTimeStats(projectId, startDate, endDate));
    }
}


